/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */
package util;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class ImageHelper {
    public static final Sprite medalSprite = new Sprite(ImageHelper.loadImage("/images/medal.png"), 24, 24);

    /**
     * Loads an image from resources
     * @param path Path of the image file
     * @return Loaded image
     * @throws RuntimeException
     */
    public static Image loadImage(String path) throws RuntimeException {
        Image image = null;

        try {
            InputStream in = Image.class.getResourceAsStream(path);
            image = Image.createImage(in);
        } catch (IOException ioe) {
            throw new RuntimeException("ImageLoader failed to load image:" + path + " " + ioe.getMessage());
        }

        return image;
    }
    
    public static Image createPixelMask(int size) {
        int imgSize = size*16;
        Image mask = Image.createImage(imgSize, imgSize);
        Graphics g = mask.getGraphics();
        g.setColor(0, 0, 0);
        for(int i = 1; i < 16; i++) {
            //vẽ chiều ngang
            g.drawLine(0, i*size, imgSize, i*size);
            //vẽ chiều dọc
            g.drawLine(i*size, 0, i*size, imgSize);
        }
        int[] rgb = new int[imgSize*imgSize];
        mask.getRGB(rgb, 0, imgSize, 0, 0, imgSize, imgSize);
        for (int i = 0; i < rgb.length; ++i) {
            if (rgb[i] == 0xffffffff) {
                rgb[i] &= 0x00ffffff;
            } else {
                rgb[i] &= 0x22000000;
            }
        }
        mask = Image.createRGBImage(rgb, imgSize, imgSize, true);
        return mask;
    }
}
